package com.ybf.user.dal.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybf.user.dal.entity.OrgAgentDO;
import com.ybf.user.dal.entity.query.OrgAgentQueryDO;
import com.ybf.user.dal.enums.YesOrNoEnum;
import com.ybf.user.dal.mapper.OrgAgentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class OrgAgentManager extends ServiceImpl<OrgAgentMapper, OrgAgentDO> {

    @Autowired
    private OrgAgentMapper orgAgentMapper;

    /**
     * 查询列表
     *
     * @author 向南
     * @date 18:11 2023/7/31
     * @param: orgId
     **/
    public List<OrgAgentDO> selectByOrgId(Long orgId) {
        if (Objects.isNull(orgId)) {
            return Collections.emptyList();
        }
        return orgAgentMapper.selectList(new LambdaUpdateWrapper<OrgAgentDO>().eq(OrgAgentDO::getOrgId, orgId));
    }

    /**
     * 查询可用的
     *
     * @author 向南
     * @date 19:06 2023/7/31
     * @param: orgId
     **/
    public List<OrgAgentDO> selectByOrgIdAndAvailable(Long orgId) {
        if (Objects.isNull(orgId)) {
            return Collections.emptyList();
        }
        Date current = new Date();
        return orgAgentMapper.selectList(new LambdaQueryWrapper<OrgAgentDO>()
                .eq(OrgAgentDO::getOrgId, orgId)
                .eq(OrgAgentDO::getStatus, YesOrNoEnum.YES.getValue())
                .le(OrgAgentDO::getStartTime, current)
                .ge(OrgAgentDO::getEndTime, current));
    }

    /**
     * 查询信息
     *
     * @author 向南
     * @date 14:27 2023/8/11
     * @param: orgId
     * @param: agentId
     **/
    public OrgAgentDO selectByOrgIdAndAgentId(Long orgId, Long agentId) {
        if (Objects.isNull(orgId) || Objects.isNull(agentId)) {
            return null;
        }
        return orgAgentMapper.selectOne(new LambdaQueryWrapper<OrgAgentDO>().eq(OrgAgentDO::getOrgId, orgId).eq(OrgAgentDO::getAgentId, agentId));
    }

    /**
     * 分页查询
     *
     * @author 向南
     * @date 14:30 2023/8/11
     * @param: queryDO
     **/
    public IPage<OrgAgentDO> selectPage(OrgAgentQueryDO queryDO) {
        return orgAgentMapper.selectPage(queryDO.getPage(), new LambdaQueryWrapper<OrgAgentDO>()
                .eq(OrgAgentDO::getOrgId, queryDO.getOrgId()));
    }
}
